<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Requests\CommentInsertRequest;
use App\Http\Controllers\Controller;

class CommentController extends Controller
{
    /*
        添加评论页面
    */
    public function getAdd(Request $request)
    {
        //判断用户是否登录
        if(empty(session('id'))){
            return redirect('/login')->with('亲,你还没有登录哦!');
        }
        //接收订单id
        $id = $request->input('id');
        //根据id查询信息
        $order = DB::table('order')
                    ->join('goods','order.goods_id','=','goods.id')
                    ->select('order.*','goods.goods_pic','goods.goods_name')
                    ->where('order.id',$id)
                    ->first();
        //获取用户头像
        $pic = DB::table('user')->where('id',session('id'))->value('pic');

        //三级菜单
        $menu = HomeListController::menu(0);

        //解析模板
        return view('home.comment.add',['menu'=>$menu,'pic'=>$pic,'order'=>$order]);
    }


    /*
        显示评论页面
    */
    public function getIndex()
    {
        $menu = HomeListController::menu(0);

        //判断用户是否登录
        if(empty(session('id'))){
            return redirect('/login')->with('error','亲,你还没有登录哦!');
        }
        //查询当前用户头像
        $pic = DB::table('user')->where('id',session('id'))->value('pic');

        //查询 用户评价
        $comment = DB::table('comment')
                    ->join('goods','comment.goods_id','=','goods.id')
                    ->join('user','comment.user_id','=','user.id')
                    ->select('comment.*','user.username','user.pic','goods.goods_name','goods.goods_price')
                    ->where('comment.user_id',session('id'))
                    ->paginate(4);

        //查询当前用户的评论条数
        $num = count(DB::table('comment')->where('user_id',session('id'))->get());

        return view('home.comment.comment',['comment'=>$comment,'pic'=>$pic,'menu'=>$menu,'num'=>$num]);
    }

    /*
        执行添加
    */
    public function postInsert(CommentInsertRequest $request)
    {
        //接收订单id
        $order_id = $request->input('order_id');
        //接收部分参数
        $data = $request->only(['goods_id','content','status']);
        $data['date'] = time();
        $data['user_id'] = session('id');

        DB::beginTransaction();
        //执行添加
        $res = DB::table('comment')->insert($data);
        if(!$res){
            DB::rollBack();
            return back();
        }
        $num = DB::table('order')->where('id',$order_id)->update(['pay'=>5]);
        if($num < 1){
            DB::rollBack();
            return back();
        }
        if($num && $res){
            DB::commit();
            return redirect('/order/orderlist');
        }
    }

    /*
        删除评论
    */
    public function getDelete(Request $request)
    {   
        //接收传过来的id
        $id = $request->input('id');

        //执行删除
        $res = DB::table('comment')->where('id',$id)->delete();
        //判断
        if($res){
            return 1;
        }else{
            return 2;
        }
    }
}
